Friend Class TH_Upload_EEPROM
    Inherits Threads.BikiThread

    Private m_data() As Byte
    Private m_Address As UInt16 = 0
    Private m_CommHandle As Comm.UsbCommunicationHandle

    Sub New(ByRef CommHandle As Comm.UsbCommunicationHandle, ByVal Data As Byte(), ByVal Address As UInt16)
        m_CommHandle = CommHandle
        m_data = Data
        m_Address = Address
    End Sub

    Friend Overloads Function Execute( _
             ByVal backGroundWorker As System.ComponentModel.BackgroundWorker, _
             ByVal e As System.ComponentModel.DoWorkEventArgs) As Boolean
        ' -------------------- Calls the inherited execute routine
        MyBase.Execute(backGroundWorker, e)

        If Not m_UsbCommHandler Is Nothing _
        AndAlso m_UsbCommHandler.IsConnected _
        AndAlso m_UsbCommHandler.Initialised Then
            OnMessage("Uploading Bikirom Settings...")

            If MyClass.UploadEEPROM = BK_return.BK_OK Then
                'm_CommHandle.ResetDevice()
                OnMessage("Uploading Bikirom Settings OK!")
                Dim f_crc As New libCRC32
                m_CommHandle.SetUsbVariable(Biki_USBvars.UV_BikEEPROM_CRC, f_crc.GetByteArrayCrc32(m_data))
            Else
                'OnMessage(Err_Decode(f_ret))
            End If

        Else
            OnMessage("Uploading Bikirom Settings failed, comms not initialised")
        End If

    End Function

    Private Function UploadEEPROM() As BK_return
        Dim f_Frame As Comm.Comm_Frame
        Dim i As ULong
        Dim f_PacketSize As UInt16 = 128 ' m_CommHandle.UsbInformation.MaxTxPacket
        Dim f_Packet(f_PacketSize - 1) As Byte
        Dim f_return As BK_return = BK_return.BK_NOT_CONNECTED

        m_CommHandle.Timeout = 2000
        Try
            For i = m_Address To UBound(m_data) + m_Address - f_PacketSize Step f_PacketSize
                Array.Copy(m_data, CLng(i - m_Address), f_Packet, 0, f_PacketSize)

                f_Frame = New Comm.Comm_Frame(Comm.Comm_Frame.BikiFrameType.ID_EEPROM_DATA, i, Comm.Comm_Frame.BikiFrameAction.WRITE, f_Packet)
                m_CommHandle.SendFrame(f_Frame, , , 10)

                OnProgress(((i - m_Address) / (UBound(m_data))) * 100)
            Next

            If ((UBound(m_data) - m_Address) Mod f_PacketSize) <> 0 Then
                ' get the final and last packet here... fool :)
                f_PacketSize = ((UBound(m_data) - m_Address) Mod f_PacketSize) + 1
                ReDim f_Packet(f_PacketSize - 1)

                Array.Copy(m_data, CLng(UBound(m_data) - f_PacketSize + 1), f_Packet, 0, f_PacketSize)

                f_Frame = New Comm.Comm_Frame(Comm.Comm_Frame.BikiFrameType.ID_EEPROM_DATA, (UBound(m_data) + m_Address) - f_PacketSize + 1, Comm.Comm_Frame.BikiFrameAction.WRITE, f_Packet)
                m_CommHandle.SendFrame(f_Frame, , , 10)

            End If
            OnProgress(100)
            f_return = BK_return.BK_OK
        Catch exComm As Comm_Exceptions
            OnMessage(exComm.Description)
            f_return = BK_return.BK_IO_ERROR_BIK
        Catch exUsb As USB_exceptions
            OnMessage(exUsb.Description)
            f_return = BK_return.BK_IO_ERROR_USB
        Finally
            m_CommHandle.Flush()
        End Try

        Return f_return
    End Function
End Class
